/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ses;

import Entity.AccountDetails;
import ses.exceptions.IllegalOrphanException;
import ses.exceptions.NonexistentEntityException;
import ses.exceptions.PreexistingEntityException;
import ses.exceptions.RollbackFailureException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import Entity.City;
import Entity.Accounts;
import java.util.ArrayList;
import javax.transaction.UserTransaction;

/**
 *
 * @author Huu Vinh
 */
public class AccountDetailsJpaController implements Serializable {

    public AccountDetailsJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(AccountDetails accountDetails) throws IllegalOrphanException, PreexistingEntityException, RollbackFailureException, Exception {
        List<String> illegalOrphanMessages = null;
        Accounts accountsOrphanCheck = accountDetails.getAccounts();
        if (accountsOrphanCheck != null) {
            AccountDetails oldAccountDetailsOfAccounts = accountsOrphanCheck.getAccountDetails();
            if (oldAccountDetailsOfAccounts != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("The Accounts " + accountsOrphanCheck + " already has an item of type AccountDetails whose accounts column cannot be null. Please make another selection for the accounts field.");
            }
        }
        if (illegalOrphanMessages != null) {
            throw new IllegalOrphanException(illegalOrphanMessages);
        }
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            City cityId = accountDetails.getCityId();
            if (cityId != null) {
                cityId = em.getReference(cityId.getClass(), cityId.getCityId());
                accountDetails.setCityId(cityId);
            }
            Accounts accounts = accountDetails.getAccounts();
            if (accounts != null) {
                accounts = em.getReference(accounts.getClass(), accounts.getAccId());
                accountDetails.setAccounts(accounts);
            }
            em.persist(accountDetails);
            if (cityId != null) {
                cityId.getAccountDetailsList().add(accountDetails);
                cityId = em.merge(cityId);
            }
            if (accounts != null) {
                accounts.setAccountDetails(accountDetails);
                accounts = em.merge(accounts);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            if (findAccountDetails(accountDetails.getAccId()) != null) {
                throw new PreexistingEntityException("AccountDetails " + accountDetails + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(AccountDetails accountDetails) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            AccountDetails persistentAccountDetails = em.find(AccountDetails.class, accountDetails.getAccId());
            City cityIdOld = persistentAccountDetails.getCityId();
            City cityIdNew = accountDetails.getCityId();
            Accounts accountsOld = persistentAccountDetails.getAccounts();
            Accounts accountsNew = accountDetails.getAccounts();
            List<String> illegalOrphanMessages = null;
            if (accountsNew != null && !accountsNew.equals(accountsOld)) {
                AccountDetails oldAccountDetailsOfAccounts = accountsNew.getAccountDetails();
                if (oldAccountDetailsOfAccounts != null) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("The Accounts " + accountsNew + " already has an item of type AccountDetails whose accounts column cannot be null. Please make another selection for the accounts field.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (cityIdNew != null) {
                cityIdNew = em.getReference(cityIdNew.getClass(), cityIdNew.getCityId());
                accountDetails.setCityId(cityIdNew);
            }
            if (accountsNew != null) {
                accountsNew = em.getReference(accountsNew.getClass(), accountsNew.getAccId());
                accountDetails.setAccounts(accountsNew);
            }
            accountDetails = em.merge(accountDetails);
            if (cityIdOld != null && !cityIdOld.equals(cityIdNew)) {
                cityIdOld.getAccountDetailsList().remove(accountDetails);
                cityIdOld = em.merge(cityIdOld);
            }
            if (cityIdNew != null && !cityIdNew.equals(cityIdOld)) {
                cityIdNew.getAccountDetailsList().add(accountDetails);
                cityIdNew = em.merge(cityIdNew);
            }
            if (accountsOld != null && !accountsOld.equals(accountsNew)) {
                accountsOld.setAccountDetails(null);
                accountsOld = em.merge(accountsOld);
            }
            if (accountsNew != null && !accountsNew.equals(accountsOld)) {
                accountsNew.setAccountDetails(accountDetails);
                accountsNew = em.merge(accountsNew);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = accountDetails.getAccId();
                if (findAccountDetails(id) == null) {
                    throw new NonexistentEntityException("The accountDetails with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            AccountDetails accountDetails;
            try {
                accountDetails = em.getReference(AccountDetails.class, id);
                accountDetails.getAccId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The accountDetails with id " + id + " no longer exists.", enfe);
            }
            City cityId = accountDetails.getCityId();
            if (cityId != null) {
                cityId.getAccountDetailsList().remove(accountDetails);
                cityId = em.merge(cityId);
            }
            Accounts accounts = accountDetails.getAccounts();
            if (accounts != null) {
                accounts.setAccountDetails(null);
                accounts = em.merge(accounts);
            }
            em.remove(accountDetails);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<AccountDetails> findAccountDetailsEntities() {
        return findAccountDetailsEntities(true, -1, -1);
    }

    public List<AccountDetails> findAccountDetailsEntities(int maxResults, int firstResult) {
        return findAccountDetailsEntities(false, maxResults, firstResult);
    }

    private List<AccountDetails> findAccountDetailsEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from AccountDetails as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public AccountDetails findAccountDetails(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(AccountDetails.class, id);
        } finally {
            em.close();
        }
    }

    public int getAccountDetailsCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from AccountDetails as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
